package com.ybkj.daijia.server.controller;

import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.server.model.CarType;
import com.ybkj.daijia.server.service.CarService;
import com.ybkj.daijia.server.service.CarTypeService;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Shine
 */
@Controller
@RequestMapping(value = "carType")
public class CarTypeController {

    @Autowired
    private CarTypeService carTypeService;
    @Autowired
    private CarService carService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ModelAndView list() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("carType")) {
            List<CarType> list = carTypeService.findAll();

            ModelAndView mv = new ModelAndView();
            mv.addObject("lists", list);
            mv.setViewName("cartype/list");

            return mv;

        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "toAdd", method = RequestMethod.GET)
    public ModelAndView add(HttpSession session) {
        ModelAndView mv = new ModelAndView();

        mv.addObject("carType", new CarType());

        mv.setViewName("cartype/add");
        return mv;

    }

    @RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
    public ModelAndView toEdit(@PathVariable("id") Long id) {
        ModelAndView mv = new ModelAndView();
        CarType carType = carTypeService.findById(id);

        mv.addObject("carType", carType);

        mv.setViewName("cartype/edit");
        return mv;
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(CarType carType, Long id, Model model) {
        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("carType")) {

            CarType findByName = carTypeService.findByName(carType.getName());
            if (findByName != null && findByName.getId() != carType.getId()) {
                model.addAttribute("message", "此类型已经存在!");
                model.addAttribute("carType", carType);
                return "cartype/edit";
            }

            carType.setName(carType.getName());
            carType.setSequence(carType.getSequence());
            carType.setUpdated(new Date());
            int i = carTypeService.update(carType);
            if (i != 0) {
            }
            return "redirect:list";

        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ModelAndView create(CarType carType, String name, Model model) {
        Subject subject = SecurityUtils.getSubject();
        ModelAndView mv = new ModelAndView();
        if (subject.isPermitted("carType")) {
            CarType c = carTypeService.findByName(name);
            if (c != null) {
                mv.addObject("message", "此类型已存在!!");
                mv.addObject("carType", new CarType());
                mv.setViewName("cartype/add");
                return mv;
            }
            carType.setName(name);
            carType.setCreated(new Date());
            carType.setUpdated(new Date());
            int i = carTypeService.save(carType);
            mv.addObject("lists", carTypeService.findAll());
            mv.setViewName("cartype/list");
            return mv;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(Long id) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("carType")) {
            Map<String, Object> result = new HashMap<String, Object>();
            Long total = carService.countCarByTypeId(id);
            if (total == 0) {
//			  carService.deleteCarType(id);		//删除关联汽车的类型

                int i = carTypeService.delete(id);

                if (i != 0) {
                    result.put("success", true);
                    result.put("message", "删除失败");
                } else {
                    result.put("success", true);
                }
            } else {
                result.put("success", false);
                result.put("message", "有车辆忙碌中,不能删除该车型");
            }

            return result;

        } else {
            throw new NoPermissionException();
        }
    }
}
